package com.chima.action;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.chima.domain.DetailJob;
import com.chima.domain.Employees;
import com.chima.domain.Job;
import com.chima.domain.PageRequestBean;
import com.chima.domain.PageResponseBean;
import com.chima.domain.TastJob;
import com.chima.service.TastJobService;
import com.opensymphony.xwork2.ActionSupport;

import flexjson.JSONSerializer;
import flexjson.transformer.DateTransformer;

public class TastJobAction extends ActionSupport {
	private String name;
	private Date creatDate;
	private Employees employee;
	private int jobstyle;
	private int state;
	private  int level;
	private int id;
	private double core;
	private String content;
	private String searchdate;
	private boolean success=false;
	
	private int start;// 开始页码
	private int limit; //每页显示
	private int page;//页数
	private List<TastJob> tastJobs;

	private TastJobService  tastJobService;
	public double getCore() {
		return core;
	}
	public void setCore(double core) {
		this.core = core;
	}
	public List<TastJob> getTastJobs() {
		return tastJobs;
	}
	public void setTastJobs(List<TastJob> tastJobs) {
		this.tastJobs = tastJobs;
	}
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public String getSearchdate() {
		return searchdate;
	}
	public void setSearchdate(String searchdate) {
		this.searchdate = searchdate;
	}


	
	
	public void setTastJobService(TastJobService tastJobService) {
		this.tastJobService = tastJobService;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Date getCreatDate() {
		return creatDate;
	}
	public void setCreatDate(Date creatDate) {
		this.creatDate = creatDate;
	}
	public Employees getEmployee() {
		return employee;
	}
	public void setEmployee(Employees employee) {
		this.employee = employee;
	}
	public int getJobstyle() {
		return jobstyle;
	}
	public void setJobstyle(int jobstyle) {
		this.jobstyle = jobstyle;
	}
	public int getState() {
		return state;
	}
	public void setState(int state) {
		this.state = state;
	}
	public int getLevel() {
		return level;
	}
	public void setLevel(int level) {
		this.level = level;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	
	public String  ListTastJob() throws Exception{
	    Employees  existEmployee =(Employees) ServletActionContext.getRequest().getSession().getAttribute("existEmployee");
	    PageRequestBean pageRequestBean = new PageRequestBean();
	    DetachedCriteria criteria = DetachedCriteria.forClass(TastJob.class);
	    if(searchdate==null || "".equals(searchdate)){
	    	 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	  	     Calendar  c = Calendar.getInstance();
	         c.set(Calendar.DAY_OF_YEAR, 1);//本年第一天
	         String first=sdf.format(c.getTime());
	         c.add(Calendar.YEAR, 1);
	         c.set(Calendar.DAY_OF_YEAR, 1);//本年最后一天
	         c.add(Calendar.DAY_OF_YEAR, -1);
	         String Last=sdf.format(c.getTime());
	         criteria.add(Restrictions.ge("creatDate", sdf.parse(first)));
			 criteria.add(Restrictions.le("creatDate", sdf.parse(Last)));
	    	
	    }
	    if(searchdate!=null){
	    	 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 
			 int day=Integer.parseInt( searchdate.substring(5,7));
			 int year=Integer.parseInt( searchdate.substring(0,4));
			String  First =getFirstDayOfMonth(year,day);
			String  last =getLastDayOfMonth(year,day);
			criteria.add(Restrictions.ge("creatDate", sdf.parse(First)));
			criteria.add(Restrictions.le("creatDate", sdf.parse(last)));
	    }
	    if(!existEmployee.getName().equals("admin")){
	    	  criteria.add(Restrictions.eq("employee.id",existEmployee.getId() ));
	    	  pageRequestBean.setEmp(existEmployee.getId());
	    }
	    pageRequestBean.setDate(searchdate);
	    pageRequestBean.setCriteria(criteria);
		pageRequestBean.setStart(start);
		pageRequestBean.setLimit(limit);
		pageRequestBean.setPage(page);
		PageResponseBean pageResponseBean = tastJobService.TastJobs(pageRequestBean);
        tastJobs=pageResponseBean.getRows();
        int total=0;
        total =pageResponseBean.getTotal();
		// 输出json内容到客户端
        JSONSerializer jsonSerializer = new JSONSerializer();
        jsonSerializer.exclude("*.class");
        jsonSerializer.transform(new DateTransformer("yyyy-MM-dd"),Date.class);
        String resultJson1 = jsonSerializer.deepSerialize(tastJobs);  
        String resultJson2 =jsonSerializer.serialize(total);
        String jsonString = "{total:"+resultJson2.toString()+",tastJobs:"+resultJson1.toString()+"}";
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
	
  }
	
	public void addTastJob() throws Exception{
		 Employees  existEmployee =(Employees) ServletActionContext.getRequest().getSession().getAttribute("existEmployee");
		    	TastJob tastJob =new TastJob();
		    	tastJob.setName(name);
		    	tastJob.setContent(content);
//		    	tastJob.setJobstyle(jobstyle);
		    	tastJob.setCore(core);
		    	tastJob.setCreatDate(creatDate);
		    	tastJob.setState(1);
//		    	tastJob.setLevel(0);
		    	tastJob.setEmployee(existEmployee);
			    tastJobService.saveTastJob(tastJob);
		    	
		this.success=true;
		JSONSerializer jsonSerializer = new JSONSerializer();
		String resultJson =jsonSerializer.serialize(success);
		String jsonString = "{success:"+resultJson.toString()+"}";
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
	}
	public void editTastJob() throws Exception{
		 Employees  existEmployee =(Employees) ServletActionContext.getRequest().getSession().getAttribute("existEmployee");
		   
		    	TastJob tastJob =new TastJob();
		    	tastJob.setName(name);
		    	tastJob.setId(id);
		    	tastJob.setContent(content);
		    	tastJob.setCore(core);
//		    	tastJob.setJobstyle(jobstyle);
		    	tastJob.setCreatDate(creatDate);
		    	tastJob.setState(1);
//		    	tastJob.setLevel(0);
		    	tastJob.setEmployee(existEmployee);
		    	tastJobService.updateTastJob(tastJob);
		    	
		this.success=true;
		JSONSerializer jsonSerializer = new JSONSerializer();
		String resultJson =jsonSerializer.serialize(success);
		String jsonString = "{success:"+resultJson.toString()+"}";
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
	}
	public String fabu() throws Exception{
		TastJob tastJob=tastJobService.findTastJobbyId(id);
		if(tastJob.getState()==1){
			tastJob.setState(2);
			tastJobService.updateTastJob(tastJob);
		}else{
			throw new RuntimeException("项目已发布或完成");
		}
		this.success=true;
		JSONSerializer jsonSerializer = new JSONSerializer();
		String resultJson =jsonSerializer.serialize(success);
		String jsonString = "{success:"+resultJson.toString()+"}";
		System.out.println(jsonString);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
	}
	 /***
	  * 得到某年某月的第一天
	  * @param year
	  * @param month
	 */
	public String getFirstDayOfMonth(int year, int month)  {
		 
		  Calendar cal = Calendar.getInstance();
		  cal.set(Calendar.YEAR, year);
		  cal.set(Calendar.MONTH, month-1);
		  cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));	 
		
		  return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
	}
  /**
	  * 得到某年某月的最后一天
	  *
	  * @param year
	  * @param month
	*/
	 public  String getLastDayOfMonth(int year, int month) throws Exception {
		 
		  Calendar cal = Calendar.getInstance();
		  cal.set(Calendar.YEAR, year);
		  cal.set(Calendar.MONTH, month-1);
		  cal.set(Calendar.DAY_OF_MONTH, 1);
		  int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		  cal.set(Calendar.DAY_OF_MONTH, value);
		 	
		  return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()); 
		 }

}
